package com.ds.lens.manager.service;

import com.ds.lens.data.common.util.HttpUtil;
import org.springframework.http.ResponseEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * @author WeiShaoying
 * @date 2019/11/19 上午10:36
 */
public class ScheduleMuteTask implements Runnable {

    private Long id;

    private Boolean mute;

    private String muteServiceUrl;

    public ScheduleMuteTask(Long id, Boolean mute, String muteServiceUrl) {
        this.id = id;
        this.mute = mute;
        this.muteServiceUrl = muteServiceUrl;
    }

    @Override
    public void run() {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("mute", mute);
        for (int i = 2; i <= 8; i = i * 2) {
            try {
                if (request(params)) {
                    return;
                }
                Thread.sleep(i * 60000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private boolean request(Map<String, Object> params) {
        ResponseEntity responseEntity = HttpUtil.get(muteServiceUrl, params, null, 1000);
        if (responseEntity.getStatusCode().value() == 200) {
            return true;
        }
        return false;
    }
}
